package psl;

import java.util.HashMap;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Mar 8, 2007
 * Time: 6:11:27 PM
 */
public class RagistrationPage extends BasePage
{
    public String msFname;
    public String msLname;
    public String msMname;
    public String msAddress;
    public String msEmail;
    public String msPhone;
    public String msPassword;
    public String msCpassword;
    public String msLoginId;
    public String msCity;
    public String msQuestion;
    public String msAnswer;
    public String msCountry;
    public String msState;

    public RagistrationPage ()
    {
        msViewPage = "RagistrationPage.jsp";
    }

    public void setRequest (HashMap fhRequest)
    {
        super.setRequest (fhRequest);
        msFname = (String) mhRequest.get ("fname");
        msLname = (String) mhRequest.get ("lname");
        msMname = (String) mhRequest.get ("mname");
        msAddress = (String) mhRequest.get ("address");
        msAnswer = (String) mhRequest.get ("answer");
        msCity = (String) mhRequest.get ("city");
        msCpassword = (String) mhRequest.get ("cpassword");
        msEmail = (String) mhRequest.get ("email");
        msPassword = (String) mhRequest.get ("password");
        msPhone = (String) mhRequest.get ("phone");
        msQuestion = (String) mhRequest.get ("question");
        msLoginId = (String) mhRequest.get ("loginid");
    }

    public void performSave ()
    {
        String lsError = validate ();
        if (lsError != null)
        {
            moUserSession.msError = lsError;
        }
        else
        {
            moUserSession.msError = null;
            String laState[] = { "state_name", "state_code" };
            String lsStateQuery = "select state_name, state_code from state_web where " +
                    "state_code=(select state_code from city_web where city_name='" + msCity + "')";
            String[][] state = DBConn.setRecordsToString (lsStateQuery, laState,"");
            String lsStatecode = null;
            msState = state[0][0];
            lsStatecode = state[1][0];
            String[] lacountry = { "country_name" };
            String lsCountryQuery = "select country_name from country_web where " +
                    "country_code=(select country_code from state_web where state_code='" + lsStatecode + "')";
            String[][] counntry = DBConn.setRecordsToString (lsCountryQuery, lacountry,"");
            msCountry = counntry[0][0];
            msPassword=Encryptor.encrypt (msPassword,AppConstants.ENCRYPTOR_kEY);
            String  lsQuery = "Insert into customer_new_web " +
                    "values (" +
                    "'" + msLoginId + "'," +
                    "'" + msFname + "'," +
                    "'" + msMname + " " + msLname + "'," +
                    "'" + msPassword + "'," +
                    "'" + msQuestion + "'," +
                    "'" + msAnswer + "'," +
                    "'" + msPhone + "'," +
                    "'" + msEmail + "'," +
                    "'" + msCountry + "'," +
                    "'" + msState + "'," +
                    "'" + msCity + "'," +
                    "'" + msAddress + "'" +
                    ")";
            moUserSession.msError=DBConn.executeUpdate(lsQuery);
            if(moUserSession.msError!=null)
            {
                moUserSession.msError +=lsQuery;
                return;
            }
            else
            {
                moUserSession.msError =null;
                mbSaveFlag=true;
                moUserSession.msConform="Register SuccessFuly!!!";
            }
        }
    }

    public String validate ()
    {
        String lsError = null;
        if (msFname == null)
        {
            lsError = "FirstName is Required";
            return lsError;
        }
        if (msLname == null)
        {
            lsError = "LastName is Required";
            return lsError;
        }
        if (msCpassword == null)
        {
            lsError = "Please Comfirm Your password ";
            return lsError;
        }
        if (msPassword == null)
        {
            lsError = "Password is Required";
            return lsError;
        }
        if (msPassword.length () < 6)
        {
            lsError = "Password Length Must be  Atleast Six";
            return lsError;
        }

        if (msLoginId == null)
        {
            lsError = "Login Id is Required";
            return lsError;
        }
        else
        {
            String SQLQuery="select cid from customer_new_web";
            ResultSet moResultSet=DBConn.getRecordFromDB (SQLQuery);
            try
            {
                while(moResultSet.next ())
                {
                    String loginId=moResultSet.getString ("cid");
                    if(loginId.equalsIgnoreCase (msLoginId))
                    {
                        lsError = msLoginId+ " is Allready Present";
                        return lsError;
                    }
                }
                moResultSet.close ();
            }
            catch (SQLException e)
            {
                e.printStackTrace ();
            }

        }
        if (msEmail != null)
        {
            if (!CommonServices.isEmail (msEmail))
            {
                lsError = "It is not valid Emailid";
                return lsError;
            }
        }
        else
        {
            lsError = "Email is Required";
            return lsError;
        }
        if (msCity == null)
        {
            lsError = "City is Required";
            return lsError;
        }
        if (msAddress == null)
        {
            lsError = "Address is Required";
            return lsError;
        }
        if (!msPassword.equals (msCpassword))
        {
            lsError = "Your Password Is Not Matching With Confirm Password";
            return lsError;
        }
        if (msPhone != null)
        {
            if (CommonServices.isNaN (msPhone))
            {
                lsError = "String is Not Allowed in Phone Number";
                return lsError;
            }
        }
        return lsError;
    }
}
